//
// fonctions.cpp for 206observ in /home/faucon_l//Work/Tek2/projet/maths/206observ
//
// Made by leo faucon
// Login   <faucon_l@epitech.net>
//
// Started on  Tue May  3 17:02:08 2011 leo faucon
// Last update Tue May 10 16:54:23 2011 yoni goldberg
//

#include <iomanip>
#include <cmath>

#include "convert.hpp"
#include "Maths.hpp"

void		Maths::moy_ari()
{
  int		ind = 0;
  float		temp = 0;

  while (ind < line)
    {
      temp += (val[ind] * coef[ind]);
      ind++;
    }
  temp /= total;
  res.push_back(temp);
}

void		Maths::moy_quad()
{
  int		ind = 0;
  float		temp = 0;

 while (ind < line)
    {
      for (int i = 0 ; i < coef[ind]; i++)
	temp += (val[ind] * val[ind]);
      ind++;
    }
 temp /= total;
 temp = sqrt(temp);
 res.push_back(temp);
}

void		Maths::moy_geo()
{
  float		tmp = 0;

  for (int i = 0 ; i < line; i++)
    for (int j = 0; j < coef[i] ; j++)
      tmp += log10(val[i]);
  tmp /= total;
  res.push_back(pow(10,tmp));
}

void		Maths::moy_harm()
{
  float		temp = 0;

  for (int i = 0 ; i < line; i++)
    for (int j = 0; j < coef[i] ; j++)
      {
	float tmp = 1;
	tmp /= val[i];
	temp += tmp;
      }
  res.push_back(total/temp);
}

void		Maths::mediane()
{
  bool		b = (total % 2 == 0) ? true : false;
  int		count = 1;
  int		temp;
  int		i = 0;
  int		j = 0;

  while (i < line)
    {
      j = 0;
      while (j < coef[i])
	{
	  count++;
	  if ((b && count >= total/2) || (!b && count >= (int)(total/2 + 1)))
	    break;
	  j++;
	}
      i++;
      if ((b && count >= total/2) || (!b && count >= (int)(total/2 + 1)))
	break;
    }
  temp = val[i];
  if (b)
    {
      temp += (j == coef[i] - 1) ? val[i + 1] : val[i];
      temp /= 2;
    }
  res.push_back(temp);
}

void		Maths::mode()
{
  int		max = 0;

  for(int i = 1; i < line ; i++)
    if (coef[max] < coef[i])
      max = i;
  res.push_back(val[max]);
}

void		Maths::variance()
{
  float		tmp = 0;

  for (int i = 0 ; i < line; i++)
    for (int j = 0; j < coef[i] ; j++)
      tmp += pow(val[i],2);
  tmp /= total;
  tmp -= pow(res[0],2);
  res.push_back(tmp);
}

void		Maths::ecart_type()
{
  res.push_back(sqrt(res[6]));
}

void		Maths::ecart_moyen()
{
  float		tmp = 0;

  for (int i = 0 ; i < line; i++)
    for (int j = 0; j < coef[i] ; j++)
      {
	float	temp = (val[i] - res[0]);
	tmp += temp < 0 ? -temp : temp;
      }
  tmp /= total;
  res.push_back(tmp);
}

void		Maths::ecart_median()
{
  float		tmp = 0;

  for (int i = 0 ; i < line; i++)
    for (int j = 0; j < coef[i] ; j++)
      {
	int	temp = (val[i] - res[4]);
	tmp += temp < 0 ? -temp : temp;
      }
  tmp /= total;
  res.push_back(tmp);
}

void		Maths::frequence()
{
  for (int i = 0; i < line ; i++)
    {
      float temp = coef[i];
      temp /= total;
      freq.push_back(temp);
    }
}

void		Maths::premier_quart()
{
  bool		b = (total % 4 == 0) ? true : false;
  int		count = 1;
  int		temp;
  int		i = 0;
  int		j = 0;

  while (i < line)
    {
      j = 0;
      while (j < coef[i])
	{
	  count++;
	  if ((b && count >= total/4) || (!b && count >= (int)(total/4)))
	    break;
	  j++;
	}
      i++;
      if ((b && count >= total/4) || (!b && count >= (int)(total/4)))
	break;
    }
  temp = val[i];
  if (b)
    {
      temp += (j == coef[i] - 1) ? val[i + 1] : val[i];
      temp /= 2;
    }
  res.push_back(temp);
}

void		Maths::troisieme_quart()
{
  bool		b = (total % 4 == 0) ? true : false;
  int		count = 1;
  int		temp;
  int		i = 0;
  int		j = 0;

  while (i < line)
    {
      j = 0;
      while (j < coef[i])
	{
	  count++;
	  if ((b && count >= (total/4) * 3) || (!b && count >= (int)((total/4 + 1) * 3)))
	    break;
	  j++;
	}
      i++;
      if ((b && count >= (total/4) * 3) || (!b && count >= (int)((total/4 + 1) * 3)))
	break;
    }
  temp = val[i];
  if (b)
    {
      temp += (j == coef[i] - 1) ? val[i + 1] : val[i];
      temp /= 2;
    }
  res.push_back(temp);
}
